//
//  STPickerArea.m
//  STPickerView
//
//  Created by https://github.com/STShenZhaoliang/STPickerView on 16/2/15.
//  Copyright © 2016年 shentian. All rights reserved.
//

#import "STPickerJob.h"

@interface STPickerJob()<UIPickerViewDataSource, UIPickerViewDelegate>{
    NSInteger indexJob1;
    NSInteger indexJob2;
}

/** 1.数据源数组 */
@property (nonatomic, strong, nullable)NSArray *arrayRoot;
/** 2.当前行业1数组 */
@property (nonatomic, strong, nullable)NSMutableArray *arrayIndustryLv1;
/** 3.当前行业2数组 */
@property (nonatomic, strong, nullable)NSMutableArray *arrayIndustryLv2;
/** 4.当前选中数组 */
@property (nonatomic, strong, nullable)NSMutableArray *arraySelected;

/** 5.行业1 */
@property (nonatomic, strong, nullable)NSString *industryLv1;
/** 6.行业2 */
@property (nonatomic, strong, nullable)NSString *industryLv2;


@end

@implementation STPickerJob

#pragma mark - --- init 视图初始化 ---

- (void)show
{
    [self.arrayIndustryLv1 removeAllObjects];
    [self.arrayIndustryLv2 removeAllObjects];
    
    // 1.获取数据
    [self.arrayRoot enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.arrayIndustryLv1 addObject:obj[@"Name"]];
    }];
    
    NSMutableArray *citys = [NSMutableArray arrayWithArray:self.arrayRoot[indexJob1][@"Items"]];
    [citys enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.arrayIndustryLv2 addObject:obj[@"Name"]];
    }];
    
    // 2.设置视图的默认属性
    _heightPickerComponent = 32;
    [self setTitle:@"请选择所在行业"];
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
    self.industryLv1 = self.arrayIndustryLv1[indexJob1];
    self.industryLv2 = self.arrayIndustryLv2[indexJob2];
    
    [self.pickerView selectRow:indexJob1 inComponent:0 animated:NO];
    [self.pickerView selectRow:indexJob2 inComponent:1 animated:NO];
    [self.pickerView reloadAllComponents];
    
    [super show];
}

- (void)setupUI
{
    indexJob1 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"indexJob1"] integerValue];
    indexJob2 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"indexJob2"] integerValue];
    
    
    // 1.获取数据
    [self.arrayRoot enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.arrayIndustryLv1 addObject:obj[@"Name"]];
    }];

    NSMutableArray *citys = [NSMutableArray arrayWithArray:self.arrayRoot[indexJob1][@"Items"]];
    [citys enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.arrayIndustryLv2 addObject:obj[@"Name"]];
    }];
        
    // 2.设置视图的默认属性
    _heightPickerComponent = 32;
    [self setTitle:@"请选择所在行业"];
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
    self.industryLv1 = self.arrayIndustryLv1[indexJob1];
    self.industryLv2 = self.arrayIndustryLv2[indexJob2];

    [self.pickerView selectRow:indexJob1 inComponent:0 animated:NO];
    [self.pickerView selectRow:indexJob2 inComponent:1 animated:NO];
    [self.pickerView reloadAllComponents];

}
#pragma mark - --- delegate 视图委托 ---

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.arrayIndustryLv1.count;
    }else{
        return self.arrayIndustryLv2.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return self.heightPickerComponent;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.arraySelected = self.arrayRoot[row][@"Items"];

        [self.arrayIndustryLv2 removeAllObjects];
        [self.arraySelected enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.arrayIndustryLv2 addObject:obj[@"Name"]];
            
        }];
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];

    }else {
        
    }

    [self reloadData];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{

    NSString *text;
    if (component == 0) {
        text =  self.arrayIndustryLv1[row];
    }else{
        text =  self.arrayIndustryLv2[row];
    }
    
    UILabel *label = [[UILabel alloc]init];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont systemFontOfSize:17]];
    [label setText:text];
    return label;


}
#pragma mark - --- event response 事件相应 ---

- (void)selectedOk
{
    NSInteger index0 = [self.pickerView selectedRowInComponent:0];
    NSInteger index1 = [self.pickerView selectedRowInComponent:1];
    
    indexJob1 = index0;
    indexJob2 = index1;
    
    [self.delegate pickerJob:self industryLv1:self.industryLv1 industryLv2:self.industryLv2 indexL1:index0 indexL2:index1];
    [super selectedOk];
}

#pragma mark - --- private methods 私有方法 ---

- (void)reloadData
{
    NSInteger index0 = [self.pickerView selectedRowInComponent:0];
    NSInteger index1 = [self.pickerView selectedRowInComponent:1];

    self.industryLv1 = self.arrayIndustryLv1[index0];
    self.industryLv2 = self.arrayIndustryLv2[index1];
    
    NSString *title = [NSString stringWithFormat:@"%@ %@", self.industryLv1, self.industryLv2];
    [self setTitle:title];

}

#pragma mark - --- setters 属性 ---

#pragma mark - --- getters 属性 ---

- (NSArray *)arrayRoot
{
    if (!_arrayRoot) {
        _arrayRoot = [Utils getIndustriesArray];

    }
    return _arrayRoot;
}

- (NSMutableArray *)arrayIndustryLv1
{
    if (!_arrayIndustryLv1) {
        _arrayIndustryLv1 = [NSMutableArray array];
    }
    return _arrayIndustryLv1;
}

- (NSMutableArray *)arrayIndustryLv2
{
    if (!_arrayIndustryLv2) {
        _arrayIndustryLv2 = [NSMutableArray array];
    }
    return _arrayIndustryLv2;
}

- (NSMutableArray *)arraySelected
{
    if (!_arraySelected) {
        _arraySelected = [NSMutableArray array];
    }
    return _arraySelected;
}

@end


